package com.foogui.demo.oneway;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.TimeUnit;

public class OneWayProducer {

    public static void main(String[] args) throws Exception {
        //1.创建消息生产者producer，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("produce_group");
        //2.指定Nameserver地址
        producer.setNamesrvAddr("localhost:9876");
        //3.启动producer
        producer.start();
        for (int i = 0; i < 3; i++) {
            //4.创建消息对象，指定主题Topic、Tag和消息体
            //arg1：消息的topic
            //arg2：消息的Tag
            //arg3：消息主体
            Message msg = new Message("topic1", "oneway", ("Hello World 单向发送消息：" + i).getBytes());
            //5.发送单向消息
            producer.sendOneway(msg);

            TimeUnit.SECONDS.sleep(1);
        }
        //6.关闭生产者producer
        producer.shutdown();
    }
}
